package com.testing.redboy.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.testing.redboy.R;
import com.testing.redboy.bean.DbInfo;
import com.testing.redboy.interfaces.CallBackA;

import java.util.List;

/**
 * Created by Administrator on 2017/2/25.
 */

public class MybsA extends BaseAdapter {

    Context context;
    List<DbInfo> all;

    //接口的实例
    private CallBackA cba;


    public void setCallBackA(CallBackA cba) {
        this.cba = cba;
    }


    public MybsA(Context context, List<DbInfo> all) {
        this.context = context;
        this.all = all;
    }

    @Override
    public int getCount() {
        return all.size();
    }

    @Override
    public Object getItem(int position) {
        return all.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        convertView = View.inflate(context, R.layout.son_item, null);

        CheckBox pickOne = (CheckBox) convertView.findViewById(R.id.pickOne);
        TextView description = (TextView) convertView.findViewById(R.id.description);
        TextView price = (TextView) convertView.findViewById(R.id.price);
        Button cut_button = (Button) convertView.findViewById(R.id.cut_button);
        Button add_button = (Button) convertView.findViewById(R.id.add_button);
        final EditText num = (EditText) convertView.findViewById(R.id.num);

        //取出对象
        final DbInfo dbInfo = all.get(position);

        pickOne.setChecked(all.get(position).isPick());
        description.setText(dbInfo.getGname());
        price.setText("￥:" + dbInfo.getPrice());
        num.setText(dbInfo.getNum());

        //加号监听
        add_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int i = Integer.parseInt(dbInfo.getNum()) + 1;

                num.setText(i + "");

                all.get(position).setNum(i + "");

                boolean pick = all.get(position).isPick();

                if (pick) {
                    cba.keepChange(position, 2);
                }
            }
        });

        //减号监听
        cut_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = Integer.parseInt(dbInfo.getNum()) - 1;

                if (i < 0) {
                    num.setText(0 + "");
                    all.get(position).setNum(0 + "");
                    boolean pick = all.get(position).isPick();
                    if (pick) {
                        cba.keepChange(position, 3);
                    }
                } else {
                    num.setText(i + "");
                    all.get(position).setNum(i + "");
                    boolean pick = all.get(position).isPick();
                    if (pick) {
                        cba.keepChange(position, 3);
                    }
                }

            }
        });

        //checkbox监听
        pickOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (all.get(position).isPick()) {
                    all.get(position).setPick(false);
                    cba.keepChange(position, 0);
                } else {
                    all.get(position).setPick(true);
                    cba.keepChange(position, 1);
                }
            }
        });

        return convertView;
    }
}
